import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;


public class Parse {
	private HashMap<Integer, PrintWriter> moviePrinters; 
	private HashMap<Integer, PrintWriter> userPrinters;
	private static final String MOVIE_OUTPUT = "movies\\";
	private static final String USER_OUTPUT = "users\\";
	private static final String TRAINING_SET = "training_set\\";
	
	public Parse() {
		moviePrinters = new HashMap<Integer, PrintWriter>();
		userPrinters = new HashMap<Integer, PrintWriter>();
	}
	
	public void parse(String address) {
		File file = new File(address);
		
	    try {
    		BufferedReader reader = new BufferedReader(new FileReader(file));
	    	try {
	    		String temp = null;
	    		temp = reader.readLine();
	    		String[] firstLine = temp.split(":");
	    		int movieNumber = Integer.parseInt(firstLine[0]);
	    		PrintWriter moviePrinter;
	    		if(moviePrinters.get(movieNumber)==null) {
	    			moviePrinter = new PrintWriter(new FileOutputStream(MOVIE_OUTPUT+"mv_"+movieNumber+".txt"), true);
	    			moviePrinters.put(movieNumber, moviePrinter);
		    		moviePrinter.println(movieNumber+":");
	    		} else {
	    			moviePrinter = moviePrinters.get(movieNumber);
	    		}
	    		
	    		while ((temp=reader.readLine())!=null) {
	    			String[] line = temp.split(",");
	    			int userId = Integer.parseInt(line[0]);
	    			int rating = Integer.parseInt(line[1]);
	    			
	    			PrintWriter userPrinter;
	    			if(userPrinters.get(userId)==null) {
	    				userPrinter = new PrintWriter(new FileOutputStream(USER_OUTPUT+"usr_"+userId+".txt"), true);
	    				userPrinters.put(userId, userPrinter);
	    				userPrinter.println(userId+":");
	    			} else {
	    				userPrinter = userPrinters.get(userId);
	    			}
	    			
	    			String ret = "";
	    			for(int i=0; i<line.length-1;i++) {
	    				ret+=line[i];
	    				if(i<line.length-2) ret+=",";
	    			}
	    			moviePrinter.println(ret);
	    			userPrinter.println(movieNumber+","+rating);
	    		}
	    	} finally {
		    	reader.close();
		    }
	    } catch (IOException ex) {
	    	System.out.println("file does not exist");
	    }
	}
	
	public void closeAllPrinters() {
		for(Integer i : moviePrinters.keySet()) {
			moviePrinters.get(i).close();
		}
		for(Integer i : userPrinters.keySet()) {
			userPrinters.get(i).close();
		}
	}
	
	public void run(int numMovies) {
		for(int i=1; i<=numMovies;i++){
			parse(getAddress(i));
		}
	}
	
	public String getAddress(int number) {
		String ret = ""+number;
		int numZeroes = 7 - ret.length();
		for(int i=1; i<=numZeroes;i++) {
			ret = "0" + ret;
		}
		return TRAINING_SET+"mv_"+ret+".txt";
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		Parse p = new Parse();
		p.run(17770);
	}
}
